﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace LotroTweeter.Repositories
{
    public class KinshipRepository
    {
        private string dataFileLocation;

        public KinshipRepository()
        {
            dataFileLocation = String.Format("KinData.xml");
        }

        public XElement Load()
        {
            XElement kinRosterOnDisk;
            if (File.Exists(dataFileLocation))
            {
                kinRosterOnDisk = XDocument.Load(dataFileLocation).Root;
            }
            else
            {
                //Nothing to load as yet
                kinRosterOnDisk = new XElement("apiresponse");
            }
            return kinRosterOnDisk;
        }

        public void Save(XElement rosterXml)
        {
            if (File.Exists(dataFileLocation))
            {
                var renameLocation = dataFileLocation + ".old";
                if (File.Exists(renameLocation))
                {
                    File.Delete(renameLocation);
                }
                File.Move(dataFileLocation,renameLocation);
            }
            var xdoc = new XDocument();
            xdoc.Add(rosterXml);
            xdoc.Save(dataFileLocation);
        }
    }
}
